{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "696b6de4-0773-424e-8b0a-66996f74f3c3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: openai in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (1.84.0)\n",
      "Requirement already satisfied: requests in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (2.32.3)\n",
      "Requirement already satisfied: tqdm in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (4.67.1)\n",
      "Requirement already satisfied: torch in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (2.7.1)\n",
      "Requirement already satisfied: pymilvus[model] in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (2.5.10)\n",
      "Requirement already satisfied: setuptools>69 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]) (78.1.1)\n",
      "Requirement already satisfied: grpcio<=1.67.1,>=1.49.1 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]) (1.67.1)\n",
      "Requirement already satisfied: protobuf>=3.20.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]) (6.31.1)\n",
      "Requirement already satisfied: python-dotenv<2.0.0,>=1.0.1 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]) (1.1.0)\n",
      "Requirement already satisfied: ujson>=2.0.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]) (5.10.0)\n",
      "Requirement already satisfied: pandas>=1.2.4 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]) (2.3.0)\n",
      "Requirement already satisfied: milvus-lite>=2.4.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]) (2.4.12)\n",
      "Requirement already satisfied: pymilvus.model>=0.3.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus[model]) (0.3.2)\n",
      "Requirement already satisfied: anyio<5,>=3.5.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (4.9.0)\n",
      "Requirement already satisfied: distro<2,>=1.7.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (1.9.0)\n",
      "Requirement already satisfied: httpx<1,>=0.23.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (0.28.1)\n",
      "Requirement already satisfied: jiter<1,>=0.4.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (0.10.0)\n",
      "Requirement already satisfied: pydantic<3,>=1.9.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (2.11.5)\n",
      "Requirement already satisfied: sniffio in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (1.3.1)\n",
      "Requirement already satisfied: typing-extensions<5,>=4.11 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from openai) (4.14.0)\n",
      "Requirement already satisfied: idna>=2.8 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from anyio<5,>=3.5.0->openai) (3.10)\n",
      "Requirement already satisfied: certifi in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from httpx<1,>=0.23.0->openai) (2025.4.26)\n",
      "Requirement already satisfied: httpcore==1.* in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from httpx<1,>=0.23.0->openai) (1.0.9)\n",
      "Requirement already satisfied: h11>=0.16 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from httpcore==1.*->httpx<1,>=0.23.0->openai) (0.16.0)\n",
      "Requirement already satisfied: annotated-types>=0.6.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai) (0.7.0)\n",
      "Requirement already satisfied: pydantic-core==2.33.2 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai) (2.33.2)\n",
      "Requirement already satisfied: typing-inspection>=0.4.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pydantic<3,>=1.9.0->openai) (0.4.1)\n",
      "Requirement already satisfied: charset_normalizer<4,>=2 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from requests) (3.4.2)\n",
      "Requirement already satisfied: urllib3<3,>=1.21.1 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from requests) (1.26.15)\n",
      "Requirement already satisfied: filelock in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from torch) (3.18.0)\n",
      "Requirement already satisfied: sympy>=1.13.3 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from torch) (1.14.0)\n",
      "Requirement already satisfied: networkx in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from torch) (3.5)\n",
      "Requirement already satisfied: jinja2 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from torch) (3.1.6)\n",
      "Requirement already satisfied: fsspec in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from torch) (2025.5.1)\n",
      "Requirement already satisfied: numpy>=1.26.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus[model]) (2.3.0)\n",
      "Requirement already satisfied: python-dateutil>=2.8.2 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus[model]) (2.9.0.post0)\n",
      "Requirement already satisfied: pytz>=2020.1 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus[model]) (2025.2)\n",
      "Requirement already satisfied: tzdata>=2022.7 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pandas>=1.2.4->pymilvus[model]) (2025.2)\n",
      "Requirement already satisfied: transformers>=4.36.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus.model>=0.3.0->pymilvus[model]) (4.52.4)\n",
      "Requirement already satisfied: onnxruntime in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus.model>=0.3.0->pymilvus[model]) (1.22.0)\n",
      "Requirement already satisfied: scipy>=1.10.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from pymilvus.model>=0.3.0->pymilvus[model]) (1.15.3)\n",
      "Requirement already satisfied: six>=1.5 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from python-dateutil>=2.8.2->pandas>=1.2.4->pymilvus[model]) (1.17.0)\n",
      "Requirement already satisfied: mpmath<1.4,>=1.1.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from sympy>=1.13.3->torch) (1.3.0)\n",
      "Requirement already satisfied: huggingface-hub<1.0,>=0.30.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]) (0.32.4)\n",
      "Requirement already satisfied: packaging>=20.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]) (25.0)\n",
      "Requirement already satisfied: pyyaml>=5.1 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]) (6.0.2)\n",
      "Requirement already satisfied: regex!=2019.12.17 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]) (2024.11.6)\n",
      "Requirement already satisfied: tokenizers<0.22,>=0.21 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]) (0.21.1)\n",
      "Requirement already satisfied: safetensors>=0.4.3 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]) (0.5.3)\n",
      "Requirement already satisfied: hf-xet<2.0.0,>=1.1.2 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from huggingface-hub<1.0,>=0.30.0->transformers>=4.36.0->pymilvus.model>=0.3.0->pymilvus[model]) (1.1.3)\n",
      "Requirement already satisfied: MarkupSafe>=2.0 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from jinja2->torch) (3.0.2)\n",
      "Requirement already satisfied: coloredlogs in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from onnxruntime->pymilvus.model>=0.3.0->pymilvus[model]) (15.0.1)\n",
      "Requirement already satisfied: flatbuffers in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from onnxruntime->pymilvus.model>=0.3.0->pymilvus[model]) (25.2.10)\n",
      "Requirement already satisfied: humanfriendly>=9.1 in /Users/xiewentao/anaconda3/envs/deepseek/lib/python3.13/site-packages (from coloredlogs->onnxruntime->pymilvus.model>=0.3.0->pymilvus[model]) (10.0)\n"
     ]
    }
   ],
   "source": [
    "!pip install --upgrade \"pymilvus[model]\" openai requests tqdm torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "cd6156a0-3cb5-4d1a-91be-ec91283f3554",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "# 从环境变量获取 DeepSeek API Key\n",
    "api_key = os.getenv(\"DEEPSEEK_API_KEY\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "88446a41-9f96-4c03-a942-fb0d4ce05ec3",
   "metadata": {},
   "outputs": [],
   "source": [
    "from glob import glob\n",
    "\n",
    "text_lines = []\n",
    "\n",
    "for file_path in glob(\"milvus_docs/en/faq/*.md\", recursive=True):\n",
    "    with open(file_path, \"r\") as file:\n",
    "        file_text = file.read()\n",
    "\n",
    "    text_lines += file_text.split(\"# \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e147f485-f978-4724-b944-6fda94dd8103",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "72"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(text_lines)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "1b6745c9-cf9f-452f-a92f-5fcf2b85c746",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'What if I failed to pull the Milvus Docker image from Docker Hub?\\n\\nIf you failed to pull the Milvus Docker image from Docker Hub, try adding other registry mirrors. \\n\\nUsers from Mainland China can add the URL \"https://registry.docker-cn.com\" to the registry-mirrors array in **/etc.docker/daemon.json**.\\n\\n```\\n{\\n  \"registry-mirrors\": [\"https://registry.docker-cn.com\"]\\n}\\n```\\n\\n###'"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "text_lines[2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a59a9ef0-f379-4168-bfb1-52b111629406",
   "metadata": {},
   "outputs": [],
   "source": [
    "from openai import OpenAI\n",
    "\n",
    "deepseek_client = OpenAI(\n",
    "    api_key=api_key,\n",
    "    base_url=\"https://api.deepseek.com/v1\",  # DeepSeek API 的基地址\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "782d7d7a-8c29-48b7-96e2-b584179089d8",
   "metadata": {},
   "outputs": [],
   "source": [
    "from pymilvus import model as milvus_model\n",
    "\n",
    "embedding_model = milvus_model.DefaultEmbeddingFunction()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "94ac1e24-9da7-4da1-9771-4e08888ae237",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "768\n",
      "[-0.04836056  0.07163018 -0.01130064 -0.03789344 -0.03320646 -0.01318444\n",
      " -0.03041711 -0.02269505 -0.02317867 -0.00426023]\n"
     ]
    }
   ],
   "source": [
    "test_embedding = embedding_model.encode_queries([\"This is a test\"])[0]\n",
    "embedding_dim = len(test_embedding)\n",
    "print(embedding_dim)\n",
    "print(test_embedding[:10])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29b21820-3e1a-4836-a79d-aff997e5fcf2",
   "metadata": {},
   "source": [
    "## 将数据加载到 Milvus"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3f3c7ae8-7528-4757-b2c1-c96808b4c52c",
   "metadata": {},
   "source": [
    "### 创建 Collection"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "a152f5bc-ef8a-4751-94c7-da0d409c4221",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n"
     ]
    }
   ],
   "source": [
    "from pymilvus import MilvusClient\n",
    "\n",
    "milvus_client = MilvusClient(uri=\"./milvus_faq.db\")\n",
    "\n",
    "collection_name = \"my_rag_collection\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "1db47613-ea5a-4f67-b8a3-54b2175d0adf",
   "metadata": {},
   "outputs": [],
   "source": [
    "if milvus_client.has_collection(collection_name):\n",
    "    milvus_client.drop_collection(collection_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "1019a97e-9aba-4ab5-968b-89b72f7351a8",
   "metadata": {},
   "outputs": [],
   "source": [
    "milvus_client.create_collection(\n",
    "    collection_name=collection_name,\n",
    "    dimension=embedding_dim,\n",
    "    metric_type=\"IP\",  # 内积距离\n",
    "    consistency_level=\"Strong\",  # 支持的值为 (`\"Strong\"`, `\"Session\"`, `\"Bounded\"`, `\"Eventually\"`)。更多详情请参见 https://milvus.io/docs/consistency.md#Consistency-Level。\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4ad20dbe-54f1-4527-aeca-fbeeb68a3df2",
   "metadata": {},
   "source": [
    "### 插入数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "f0713273-7992-41cf-a135-c58be06cf7a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Creating embeddings:   0%|                                                                                 | 0/72 [00:00<?, ?it/s]huggingface/tokenizers: The current process just got forked, after parallelism has already been used. Disabling parallelism to avoid deadlocks...\n",
      "To disable this warning, you can either:\n",
      "\t- Avoid using `tokenizers` before the fork if possible\n",
      "\t- Explicitly set the environment variable TOKENIZERS_PARALLELISM=(true | false)\n",
      "Creating embeddings: 100%|████████████████████████████████████████████████████████████████████| 72/72 [00:00<00:00, 397355.12it/s]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'insert_count': 72, 'ids': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71], 'cost': 0}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "data = []\n",
    "\n",
    "doc_embeddings = embedding_model.encode_documents(text_lines)\n",
    "\n",
    "for i, line in enumerate(tqdm(text_lines, desc=\"Creating embeddings\")):\n",
    "    data.append({\"id\": i, \"vector\": doc_embeddings[i], \"text\": line})\n",
    "\n",
    "milvus_client.insert(collection_name=collection_name, data=data)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ee45863-1208-4a65-82f5-62e9a0d938a4",
   "metadata": {},
   "source": [
    "## 构建 RAG"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "48fa2d35-89e5-46e7-96ad-e5f1a98f77c2",
   "metadata": {},
   "source": [
    "### 检索查询数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d75ad70-ca6a-423b-8523-b3909ca20b82",
   "metadata": {},
   "source": [
    "我们指定一个关于 Milvus 的常见问题。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "f8a84366-c305-4973-90fc-debb0b4ed227",
   "metadata": {},
   "outputs": [],
   "source": [
    "question = \"What factors impact CPU usage?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f02f8c9f-c409-49fa-bffe-f5477d4b9ab1",
   "metadata": {},
   "outputs": [],
   "source": [
    "search_res = milvus_client.search(\n",
    "    collection_name=collection_name,\n",
    "    data=embedding_model.encode_queries(\n",
    "        [question]\n",
    "    ),  # 将问题转换为嵌入向量\n",
    "    limit=3,  # 返回前3个结果\n",
    "    search_params={\"metric_type\": \"IP\", \"params\": {}},  # 内积距离\n",
    "    output_fields=[\"text\"],  # 返回 text 字段\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f98ee6a1-93cc-44c6-920f-60f2cfcb361a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\n",
      "    [\n",
      "        \"What factors impact CPU usage?\\n\\nCPU usage increases when Milvus is building indexes or running queries. In general, index building is CPU intensive except when using Annoy, which runs on a single thread.\\n\\nWhen running queries, CPU usage is affected by `nq` and `nprobe`. When `nq` and `nprobe` are small, concurrency is low and CPU usage stays low.\\n\\n###\",\n",
      "        0.7029333114624023\n",
      "    ],\n",
      "    [\n",
      "        \"How do I know if my CPU supports Milvus?\\n\\n{{fragments/cpu_support.md}}\\n\\n###\",\n",
      "        0.4407927691936493\n",
      "    ],\n",
      "    [\n",
      "        \"Does simultaneously inserting data and searching impact query performance?\\n\\nInsert operations are not CPU intensive. However, because new segments may not have reached the threshold for index building, Milvus resorts to brute-force search\\u2014significantly impacting query performance.\\n\\nThe `rootcoord.minSegmentSizeToEnableIndex` parameter determines the index-building threshold for a segment, and is set to 1024 rows by default. See [System Configuration](system_configuration.md) for more information.\\n\\n###\",\n",
      "        0.33266299962997437\n",
      "    ]\n",
      "]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "retrieved_lines_with_distances = [\n",
    "    (res[\"entity\"][\"text\"], res[\"distance\"]) for res in search_res[0]\n",
    "]\n",
    "print(json.dumps(retrieved_lines_with_distances, indent=4))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7df3b7f7-dce4-43ec-8927-bccd96bee381",
   "metadata": {},
   "source": [
    "### 使用 LLM 获取 RAG 响应"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ddd772d7-367e-4399-b91b-07f0d89c57d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "context = \"\\n\".join(\n",
    "    [line_with_distance[0] for line_with_distance in retrieved_lines_with_distances]\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "950f28de-fb8d-42e4-9f86-f67eea035613",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'What factors impact CPU usage?\\n\\nCPU usage increases when Milvus is building indexes or running queries. In general, index building is CPU intensive except when using Annoy, which runs on a single thread.\\n\\nWhen running queries, CPU usage is affected by `nq` and `nprobe`. When `nq` and `nprobe` are small, concurrency is low and CPU usage stays low.\\n\\n###\\nHow do I know if my CPU supports Milvus?\\n\\n{{fragments/cpu_support.md}}\\n\\n###\\nDoes simultaneously inserting data and searching impact query performance?\\n\\nInsert operations are not CPU intensive. However, because new segments may not have reached the threshold for index building, Milvus resorts to brute-force search—significantly impacting query performance.\\n\\nThe `rootcoord.minSegmentSizeToEnableIndex` parameter determines the index-building threshold for a segment, and is set to 1024 rows by default. See [System Configuration](system_configuration.md) for more information.\\n\\n###'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "context"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "fe0c5ab7-025b-460d-81c4-a6fba8695b91",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'What factors impact CPU usage?'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "question"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "a4ccd97e-4828-4f7a-a1df-cf8251bac924",
   "metadata": {},
   "outputs": [],
   "source": [
    "SYSTEM_PROMPT = \"\"\"\n",
    "Human: 你是一个 AI 助手。你能够从提供的上下文段落片段中找到问题的答案。\n",
    "\"\"\"\n",
    "USER_PROMPT = f\"\"\"\n",
    "请使用以下用 <context> 标签括起来的信息片段来回答用 <question> 标签括起来的问题。最后追加原始回答的中文翻译，并用 <translated>和</translated> 标签标注。\n",
    "<context>\n",
    "{context}\n",
    "</context>\n",
    "<question>\n",
    "{question}\n",
    "</question>\n",
    "<translated>\n",
    "</translated>\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b923fd8f-fcfd-4e56-9b0b-7696abb76fd2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'\\n请使用以下用 <context> 标签括起来的信息片段来回答用 <question> 标签括起来的问题。最后追加原始回答的中文翻译，并用 <translated>和</translated> 标签标注。\\n<context>\\nWhat factors impact CPU usage?\\n\\nCPU usage increases when Milvus is building indexes or running queries. In general, index building is CPU intensive except when using Annoy, which runs on a single thread.\\n\\nWhen running queries, CPU usage is affected by `nq` and `nprobe`. When `nq` and `nprobe` are small, concurrency is low and CPU usage stays low.\\n\\n###\\nHow do I know if my CPU supports Milvus?\\n\\n{{fragments/cpu_support.md}}\\n\\n###\\nDoes simultaneously inserting data and searching impact query performance?\\n\\nInsert operations are not CPU intensive. However, because new segments may not have reached the threshold for index building, Milvus resorts to brute-force search—significantly impacting query performance.\\n\\nThe `rootcoord.minSegmentSizeToEnableIndex` parameter determines the index-building threshold for a segment, and is set to 1024 rows by default. See [System Configuration](system_configuration.md) for more information.\\n\\n###\\n</context>\\n<question>\\nWhat factors impact CPU usage?\\n</question>\\n<translated>\\n</translated>\\n'"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "USER_PROMPT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5bd36d85-ddb6-4e56-bfa4-eb9a5dcc36b0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The factors that impact CPU usage include:\n",
      "\n",
      "1. **Index Building**: CPU usage increases when Milvus is building indexes, except when using Annoy which runs on a single thread.\n",
      "2. **Query Execution**: CPU usage is affected by the parameters `nq` (number of query vectors) and `nprobe` (number of clusters to search). When these values are small, concurrency is low, and CPU usage remains low.\n",
      "\n",
      "<translated>\n",
      "影响 CPU 使用率的因素包括：\n",
      "\n",
      "1. **索引构建**：当 Milvus 构建索引时，CPU 使用率会增加，但使用 Annoy 时除外，因为 Annoy 在单线程上运行。\n",
      "2. **查询执行**：CPU 使用率受参数 `nq`（查询向量数量）和 `nprobe`（搜索的聚类数量）的影响。当这些值较小时，并发性较低，CPU 使用率也会保持较低水平。\n",
      "</translated>\n"
     ]
    }
   ],
   "source": [
    "response = deepseek_client.chat.completions.create(\n",
    "    model=\"deepseek-chat\",\n",
    "    messages=[\n",
    "        {\"role\": \"system\", \"content\": SYSTEM_PROMPT},\n",
    "        {\"role\": \"user\", \"content\": USER_PROMPT},\n",
    "    ],\n",
    ")\n",
    "print(response.choices[0].message.content)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be4e49cf-6de1-4d1d-91d4-da10294a33b2",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
